package com.lastproject.web.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.lastproject.dto.AnswerDTO;
import com.lastproject.dto.CategoryDTO;
import com.lastproject.dto.EmployeeDTO;
import com.lastproject.dto.QuestionDTO;
import com.lastproject.dto.QuestionOptionDTO;
import com.lastproject.dto.SubcategoryDTO;
import com.lastproject.dto.SurveyDTO;
import com.lastproject.dto.TopicDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.AnswerDAORemote;
import com.lastproject.ejb.dao.CategoryDAORemote;
import com.lastproject.ejb.dao.EmployeeDAORemote;
import com.lastproject.ejb.dao.QuestionDAORemote;
import com.lastproject.ejb.dao.QuestionOptionDAORemote;
import com.lastproject.ejb.dao.SubcategoryDAORemote;
import com.lastproject.ejb.dao.SurveyDAORemote;
import com.lastproject.ejb.dao.TopicDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;

///**
// * Session Bean implementation class AnswerStatsBean
// */
@ManagedBean(name = "surveyAnswerStatsBean")
@RequestScoped
public class SurveyAnswerStatsBean {

	@ManagedProperty(value = "#{param.idSurvey}")
    private int idSurv;
	
	private String status;
	
	private List<QuestionDTO> questionDtoList;
	
	@ManagedProperty(value = "#{surveyQOBean}")
	private SurveyQOBean surveyQOBean;
	
	@ManagedProperty(value = "#{surveyQOBeanList}")
	private List<SurveyQOBean> surveyQOBeanList = new ArrayList<SurveyQOBean>();
	
	@ManagedProperty(value = "#{surveyQOBeanArr}")
	private SurveyQOBean[] surveyQOBeanArr;
	
	public SurveyQOBean[] getSurveyQOBeanArr() {
		return surveyQOBeanArr;
	}

	

	@ManagedProperty(value = "#{questionOptionDtoList}")
	private List<QuestionOptionDTO> questionOptionDtoList;
	
	public int getIdSurv() {
		return idSurv;
	}

	public void setIdSurv(int idSurv) {
		this.idSurv = idSurv;
//		this.getSurveyInfo(idSurv);
	}

	@ManagedProperty(value = "#{userDTO}")
	private UserDTO userDTO;
	@EJB
	UserDAORemote userDao;
	
	@ManagedProperty(value = "#{employeeDTO}")
	private EmployeeDTO employeeDTO;
	@EJB
	EmployeeDAORemote employeeDao;

	@ManagedProperty(value = "#{surveyDTO}")
	private SurveyDTO surveyDTO;
	@EJB
	SurveyDAORemote surveyDao;

	@ManagedProperty(value = "#{topicDTO}")
	private TopicDTO topicDTO;
	@EJB
	TopicDAORemote topicDao;

	@ManagedProperty(value = "#{categoryDTO}")
	private CategoryDTO categoryDTO;
	@EJB
	CategoryDAORemote categoryDao;

	@ManagedProperty(value = "#{subcategoryDTO}")
	private SubcategoryDTO subcategoryDTO;
	@EJB
	SubcategoryDAORemote subcategoryDao;

	@ManagedProperty(value = "#{questionDTO}")
	private QuestionDTO questionDTO;
	@EJB
	QuestionDAORemote questionDao;

	@ManagedProperty(value = "#{questionOptionDTO}")
	private QuestionOptionDTO questionOptionDTO;
	@EJB
	QuestionOptionDAORemote questionOptionDao;

	@ManagedProperty(value = "#{answerDTO}")
	private AnswerDTO answerDTO;
	@EJB
	AnswerDAORemote answerDao;

	public SurveyAnswerStatsBean() {

	}
	
//	@PostConstruct
//	public void postConstr() {
//		questionDTO = questionDao.findById(idSurv);
////		try{
////			int idSurv = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idSurv").toString());
////			this.idSurv = idSurv;
////		} catch(NullPointerException e){
////			e.printStackTrace();
////		}
////		questionDTO = questionDao.findById(idSurvey);
////		this.idSurv = idSurvey;
//		
//	}
	

	public SurveyAnswerStatsBean(QuestionDTO questionDTO) {
		this.questionDTO = questionDTO;
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public SurveyDTO getSurveyDTO() {
		return surveyDTO;
	}

	public void setSurveyDTO(SurveyDTO surveyDTO) {
		this.surveyDTO = surveyDTO;
	}

	public TopicDTO getTopicDTO() {
		return topicDTO;
	}

	public void setTopicDTO(TopicDTO topicDTO) {
		this.topicDTO = topicDTO;
	}

	public CategoryDTO getCategoryDTO() {
		return categoryDTO;
	}

	public void setCategoryDTO(CategoryDTO categoryDTO) {
		this.categoryDTO = categoryDTO;
	}

	public SubcategoryDTO getSubcategoryDTO() {
		return subcategoryDTO;
	}

	public void setSubcategoryDTO(SubcategoryDTO subcategoryDTO) {
		this.subcategoryDTO = subcategoryDTO;
	}

	public QuestionDTO getQuestionDTO() {
		return questionDTO;
	}

	public void setQuestionDTO(QuestionDTO questionDTO) {
		this.questionDTO = questionDTO;
	}

	public QuestionOptionDTO getQuestionOptionDTO() {
		return questionOptionDTO;
	}

	public void setQuestionOptionDTO(QuestionOptionDTO questionOptionDTO) {
		this.questionOptionDTO = questionOptionDTO;
	}

	public AnswerDTO getAnswerDTO() {
		return answerDTO;
	}

	public void setAnswerDTO(AnswerDTO answerDTO) {
		this.answerDTO = answerDTO;
	}
	
	public SurveyAnswerStatsBean(int idSurv) {
		this.idSurv = idSurv;
	}
	
	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public void getSurveyInfo(int idSurv) {
		this.setSurveyDTO(surveyDao.findById(idSurv));
		
		System.out.println("Survey user ID: " + this.getSurveyDTO().getIdUser());
		System.out.println("Survey topic ID: " + this.getSurveyDTO().getIdTopic());
		System.out.println("Survey ID: " + this.getSurveyDTO().getId());
		System.out.println("Survey title: " + this.getSurveyDTO().getTitle());
		System.out.println("Survey start date: " + this.getSurveyDTO().getStartDate().toString());
//		this.setUserDTO(userDao.findCreatorUserBySurvey(idSurv));
//		System.out.println(this.getUserDTO().getFirstname()+" "+this.getUserDTO().getLastname());
		
	}
	
//	public List<QuestionDTO> getQuestionListForSurvey(int id) {
//		List<QuestionDTO> questionDtoList = null;
//		questionDtoList = questionDao.getQuestionListBySurveyId(id);
//		return questionDtoList;
//	}
	
	
	public String getStatus() {
		Calendar calStart = Calendar.getInstance();
		Calendar calEnd = Calendar.getInstance();
		
		Calendar calToday = Calendar.getInstance();
		try{
		calStart.setTime(this.getSurveyDTO().getStartDate());
		calEnd.setTime(this.getSurveyDTO().getEndDate());
		
		if (calStart.after(calToday)) {
			status = "not yet available";
		} else if (calEnd.after(calToday) || calEnd.equals(calToday)) {
			status = "ongoing";
		} else if (calEnd.before(calToday)) {
			status = "completed";
		}
		} catch(NullPointerException e){
			
		}
		
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<QuestionDTO> getQuestionDtoList() {
		return questionDtoList;
	}

	public void setQuestionDtoList(List<QuestionDTO> questionDtoList) {
		this.questionDtoList = questionDtoList;
	}

	public List<QuestionOptionDTO> getQuestionOptionDtoList() {
		return questionOptionDtoList;
	}

	public void setQuestionOptionDtoList(List<QuestionOptionDTO> questionOptionDtoList) {
		this.questionOptionDtoList = questionOptionDtoList;
	}
	
	public void setSurveyQOBeanArr(SurveyQOBean[] surveyQOBeanArr) {
		this.surveyQOBeanArr = surveyQOBeanArr;
	}

	public List<SurveyQOBean> getSurveyQOBeanList() {
		return surveyQOBeanList;
	}

	public void setSurveyQOBeanList(List<SurveyQOBean> surveyQOBeanList) {
		this.surveyQOBeanList = surveyQOBeanList;
	}

	public SurveyQOBean getSurveyQOBean() {
		return surveyQOBean;
	}

	public void setSurveyQOBean(SurveyQOBean surveyQOBean) {
		this.surveyQOBean = surveyQOBean;
	}

}
